package com.zmn.plat.services.impl.servcateory;

import com.zmn.common.dto.KvDTO;
import com.zmn.common.dto.VtDTO;
import com.zmn.common.utils.collection.CollectionUtil;
import com.zmn.common.utils.number.NumberUtil;
import com.zmn.plat.common.constant.ProductConsts;
import com.zmn.plat.common.dictionary.PlatErrorCodeDict;
import com.zmn.plat.common.exception.ParamOutOfRangeException;
import com.zmn.plat.common.exception.PlatException;
import com.zmn.plat.model.entity.category.BaseCategory;
import com.zmn.plat.model.entity.servcategory.ServAssCategory;
import com.zmn.plat.model.entity.servcategory.ServAssCategoryQuery;
import com.zmn.plat.model.entity.servcategory.ServCategory;
import com.zmn.plat.model.entity.servcategory.ServTariffAssCategoryQuery;
import com.zmn.plat.model.vo.servcategory.CategoryProductGroupVO;
import com.zmn.plat.model.vo.servcategory.ServAssCategoryVO;
import com.zmn.plat.model.vo.servcategory.ServCategoryVO;
import com.zmn.plat.persistence.interfaces.servcategory.ServAssCategoryDao;
import com.zmn.plat.services.interfaces.servcategory.ServAssCategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * @author duanzuocai
 * @version v1.0 @类说明：
 * @since 2018/10/23 14:58
 */
@Service
@Slf4j
public class ServAssCategoryServiceImpl implements ServAssCategoryService {
    @Resource
    ServAssCategoryDao servAssCategoryDao;

    @Override
    public ServAssCategory findByKey(Integer groupId) {
        return servAssCategoryDao.findByKey(groupId);
    }

    @Override
    public VtDTO findProductGroupVtByShowCategId(Integer servCategId, Integer showCategTwoId, Integer bizType) throws ParamOutOfRangeException, NullPointerException {
        if (NumberUtil.isNullOrZero(servCategId) || NumberUtil.isNullOrZero(showCategTwoId)) {
            log.error("服务分类ID或前台产品二级分类不能为空, servCategId:{}, showCategTwoId:{}", servCategId, showCategTwoId);
            throw new PlatException(PlatErrorCodeDict.ERROR_CODE_PANAM_NULL);
        }

        if (!Objects.equals(bizType, ProductConsts.BIZ_TYPE_C) && !Objects.equals(bizType, ProductConsts.BIZ_TYPE_F) && !Objects.equals(bizType, ProductConsts.BIZ_TYPE_B)) {
            log.error("业务类型超出取值范围, bizType:{}，errorCode:{}", bizType, PlatErrorCodeDict.ERROR_CODE_PARAM_OUT_OF_RANGE_ERROR);
            throw new PlatException(PlatErrorCodeDict.ERROR_CODE_PARAM_OUT_OF_RANGE_ERROR);
        }

        VtDTO vtDTO = servAssCategoryDao.findProductGroupVtByShowCategId(servCategId, showCategTwoId, bizType);
        if (Objects.isNull(vtDTO) || NumberUtil.isNullOrZero(vtDTO.getValue())) {
            log.error("servCategId：{}，showCategTwoId：{}，bizType：{}该服务品类下未配置产品组", servCategId, showCategTwoId, bizType);
            throw new PlatException(PlatErrorCodeDict.ERROR_CODE_SERCATEGORY_DOESNOT_HAVE_PRODUCT_GROUP);
        }

        return vtDTO;
    }

    @Override
    public List<BaseCategory> findCategoryOneByServId(Integer servId) {
        return servAssCategoryDao.findCategoryOneByServId(servId);
    }

    @Override
    public List<BaseCategory> findCategoryOneByServIdAndShowType(Integer servId, Integer showType) {
        return servAssCategoryDao.findCategoryOneByServIdAndShowType(servId, showType);
    }

    @Override
    public List<BaseCategory> findCategoryTwoByServId(Integer servId) {
        return servAssCategoryDao.findCategoryTwoByServId(servId);
    }

    @Override
    public List<BaseCategory> findCategoryTwoByServIdAndShowType(Integer servId, Integer showType) {
        return servAssCategoryDao.findCategoryTwoByServIdAndShowType(servId, showType);
    }

    @Override
    public List<BaseCategory> findCategoryTwoByServIdAndBrandIdAndShowType(Integer servId, Integer brandId, Integer showType) {
        return servAssCategoryDao.findCategoryTwoByServIdAndBrandIdAndShowType(servId, brandId, showType);
    }

    @Override
    public List<ServCategory> findServCategoryByBrandIdAndShowType(Integer brandId, Integer showType) {
        return servAssCategoryDao.findServCategoryByBrandIdAndShowType(brandId, showType);
    }

    @Override
    public List<KvDTO> findCategFromProduct(Integer servCategId, Integer categOneId) {
        if (servCategId == null) {
            return Collections.emptyList();
        }
        return servAssCategoryDao.findCategFromProduct(servCategId, categOneId);
    }

    @Override
    public List<ServAssCategoryVO> listByPerTime(Date perTime) {
        return servAssCategoryDao.listByPerTime(perTime);
    }

    @Override
    public List<ServAssCategoryVO> listPageByQuery(ServAssCategoryQuery query) {
        int count = countByQuery(query);
        query.setTotalCount(count);

        return servAssCategoryDao.listPageByQuery(query);
    }

    @Override
    public List<ServAssCategoryVO> listPageGroupByCategOneId(ServTariffAssCategoryQuery query) {
        int count = countGroupByCategOneId(query);
        query.setTotalCount(count);
        return servAssCategoryDao.listPageGroupByCategOneId(query);
    }

    @Override
    public List<ServAssCategoryVO> listByQuery(ServAssCategoryQuery query) {
        return servAssCategoryDao.listByQuery(query);
    }

    @Override
    public List<KvDTO<Integer, String>> listCateOneByQuery(ServAssCategoryQuery query) {
        List<ServAssCategoryVO> list = listByQuery(query);

        List<KvDTO<Integer, String>> dtos = new ArrayList<>();
        for (ServAssCategoryVO vo : list) {
            KvDTO<Integer, String> dto = new KvDTO(vo.getCategOneId(), vo.getCategOneName());

            if (dtos.contains(dto)) {
                continue;
            }

            dtos.add(dto);
        }

        return dtos;
    }

    @Override
    public List<CategoryProductGroupVO> listCategoryAndProductGroup() {
        return servAssCategoryDao.listCategoryAndProductGroup();
    }

    @Override
    public List<Integer> listProductGroupIdOfUsed(List<Integer> groupIds) {
        if (CollectionUtil.isNullOrEmpty(groupIds)) {
            log.error("#groupId empty error; groupIds:{}", groupIds);
            return Collections.emptyList();
        }

        return servAssCategoryDao.listProductGroupIdOfUsed(groupIds);
    }

    @Override
    public List<ServCategoryVO> listClassificationLevelTwo() {
        return servAssCategoryDao.listClassificationLevelTwo();
    }

    @Override
    public List<ServCategoryVO> listClassificationLevelTwoForShow() {
        return servAssCategoryDao.listClassificationLevelTwoForShow();
    }

    @Override
    public List<ServCategoryVO> findServAssCategoryByServIdAndShowType(Integer servCategId, Integer showType) {
        return servAssCategoryDao.findServAssCategoryByServIdAndShowType(servCategId, showType);
    }

    @Override
    public Integer countByQuery(ServAssCategoryQuery query) {
        return servAssCategoryDao.countByQuery(query);
    }

    @Override
    public Integer countGroupByCategOneId(ServTariffAssCategoryQuery query) {
        return servAssCategoryDao.countGroupByCategOneId(query);
    }

    @Override
    public void updateDesc(Integer id, String desc) {
        servAssCategoryDao.updateDesc(id, desc);
    }

    @Override
    public void updateDisclaimer(Integer id, String disclaimer, String updater) {
        servAssCategoryDao.updateDisclaimer(id, disclaimer, updater);
    }

    @Override
    public void updatePerBizTypeGroup(ServAssCategory servAssCategory) {
        if (NumberUtil.isNullOrZero(servAssCategory.getGroupId())) {
            return;
        }

        servAssCategoryDao.updatePerBizTypeGroup(servAssCategory);
    }

    @Override
    public Integer updateBizTypeGroup(ServAssCategory servAssCategory) {
        if (NumberUtil.isNullOrZero(servAssCategory.getGroupId())) {
            throw new PlatException(PlatErrorCodeDict.ERROR_CODE_PANAM_NULL);
        }
        return servAssCategoryDao.updateBizTypeGroup(servAssCategory);
    }

    @Override
    public void updateFaultUpdaterAndTimeByServCategIdAndCategOneId(ServAssCategoryQuery servAssCategoryQuery) {
        servAssCategoryDao.updateFaultUpdaterAndTimeByServCategIdAndCategOneId(servAssCategoryQuery);
    }

    @Override
    public Integer updateProductGroupByKeyBatch(List<ServAssCategory> servAssCategoryList) {
        return servAssCategoryDao.updateProductGroupByKeyBatch(servAssCategoryList);
    }

    @Override
    public void updateFaultByGroupId(Integer groupId, String fault, String operator) {
        if (NumberUtil.isNullOrZero(groupId)) {
            throw new PlatException(PlatErrorCodeDict.ERROR_CODE_PANAM_NULL);
        }
        servAssCategoryDao.updateFaultByGroupId(groupId, fault, operator);
    }

    @Override
    public void updateEngineerAcceptProjectAndUserAcceptProjectByGroupId(Integer groupId, String engineer, String user, String operator) {
        if (NumberUtil.isNullOrZero(groupId)) {
            throw new PlatException(PlatErrorCodeDict.ERROR_CODE_PANAM_NULL);
        }
        servAssCategoryDao.updateEngineerAcceptProjectAndUserAcceptProjectByGroupId(groupId, engineer, user, operator);
    }

    @Override
    public void updateServiceContentByGroupId(Integer groupId, String serviceContent, String operator) {
        if (NumberUtil.isNullOrZero(groupId)) {
            throw new PlatException(PlatErrorCodeDict.ERROR_CODE_PANAM_NULL);
        }
        servAssCategoryDao.updateServiceContentByGroupId(groupId, serviceContent, operator);
    }

    @Override
    public void updateTstDripByGroupId(Integer groupId, String tstDrip, String operator) {
        if (NumberUtil.isNullOrZero(groupId)) {
            throw new PlatException(PlatErrorCodeDict.ERROR_CODE_PANAM_NULL);
        }
        servAssCategoryDao.updateTstDripByGroupId(groupId, tstDrip, operator);
    }

    @Override
    public Integer insert(ServAssCategory category) {
        return servAssCategoryDao.insert(category);
    }

    @Override
    public Integer insertBatch(List<ServAssCategory> list) {
        return servAssCategoryDao.insertBatch(list);
    }

    @Override
    public Integer deleteByKeys(List<Integer> ids) {
        return servAssCategoryDao.deleteByKeys(ids);
    }

    @Override
    public Integer deleteByServId(Integer servCategId, Integer showType) {
        return servAssCategoryDao.deleteByServId(servCategId, showType);
    }
}
